// Selenium Server Fx Extension
swfobject.realEmbedSWF = swfobject.embedSWF;

swfobject.embedSWF = function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj) {
    var qString = $H(parObj).toQueryString();
    var newSWFUrlStr = "runtimeloading.swf?automationswfurl=" + swfUrlStr + "?" + qString;
    return swfobject.realEmbedSWF(newSWFUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj);
};

function toAbsURL(s) {
    if (s.indexOf("http:") > -1 || s.charAt(0) == "/") return s;
    var l = location, p, f;
    p = l.pathname.replace(/\/[^\/]*$/, '');
    f = s.match(/\.\.\//g);
    if (f) {
        s = s.substring(f.length * 3);
        for (i = f.length; i--;) {
            p = p.substring(0, p.lastIndexOf('/'));
        }
    }
    return (p + '/' + s);
}

function addSeleniumFlexEmbedListener() {
    var embedElements = document.embeds;
    for (var i = 0; i < embedElements.length; i++) {
        var node = embedElements[i];
        var newObj = createObject(node, i);
        var parent = node.parentNode;
        if (parent) {
            node.parentNode.removeChild(node);
        }
        parent.appendChild(newObj);
    }
}

function createObject(swf_obj, index) {
    var tembed = document.createElement('embed');
    var width = "100%";
    var height = "100%";
    var name = index;
    var flashvars = "";
    var allowscriptaccess = "sameDomain";

    if (swf_obj.width && swf_obj.width != "") {
        width = swf_obj.width;
    }

    if (swf_obj.height && swf_obj.height != "") {
        height = swf_obj.height;
    }

    if (swf_obj.name && swf_obj.name != "") {
        name = swf_obj.name + name;
    }

    if (swf_obj.flashvars && swf_obj.flashvars != "") {
        flashvars = swf_obj.flashvars;
    }

    if (swf_obj.allowscriptaccess && swf_obj.allowscriptaccess != "") {
        allowscriptaccess = swf_obj.allowscriptaccess;
    }

    tembed.setAttribute('src', "/selenium-server/core/runtimeloading.swf?showrecorder=true&automationswfurl=" + toAbsURL(swf_obj.src));
    tembed.setAttribute('width', width);
    tembed.setAttribute('height', height);
    tembed.setAttribute('quality', 'high');
    tembed.setAttribute('pluginspage', 'http://www.macromedia.com/go/getflashplayer/');
    tembed.setAttribute('type', 'application/x-shockwave-flash');
    tembed.setAttribute('wmode', 'transparent');
    tembed.setAttribute('name', name);
    tembed.setAttribute('id', "selenium_flex_automation");
    if (flashvars != "")
        tembed.setAttribute('flashvars', flashvars);
    if (allowscriptaccess != "")
        tembed.setAttribute('allowscriptaccess', allowscriptaccess);
    return tembed;
}


if (window.addEventListener) {
    window.addEventListener("load", addSeleniumFlexEmbedListener, false);// firefox
} else if (window.attachEvent) {
    window.attachEvent("onload", addSeleniumFlexEmbedListener);	// IE
}

Selenium.prototype.getInvocationResponse = function(commandXML) {
    var element = this.browserbot.findElement("selenium_flex_automation");
    var response = element.getInvocationResponse(commandXML);
    if(response["error"] && response["error"] != "0") {
        throw new SeleniumError(response["message"]);
    } else {
        return response["result"]==null?"null":response["result"];
    }
}

Selenium.prototype.getRequestedData = function(commandXML) {
    var element = this.browserbot.findElement("selenium_flex_automation");
    var response = element.getRequestedData(commandXML);
    if(response["error"] && response["error"] != "0") {
        throw new SeleniumError(response["message"]);
    } else {
        return response["result"]==null?"null":response["result"];
    }
}

Selenium.prototype.getElementsByFlexId = function(id) {
    var element = this.browserbot.findElement("selenium_flex_automation");
    return element.getElementsByFlexId(id);
}

